package com.briup.jz.service.impl;

import com.briup.jz.bean.Article;
import com.briup.jz.bean.ArticleExample;

import com.briup.jz.bean.Category;
import com.briup.jz.bean.CategoryExample;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.dao.ArticleMapper;

import com.briup.jz.dao.extend.ArticleExtendMapper;
import com.briup.jz.service.IArticleService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ArticleServiceImpl implements IArticleService {


//    依赖注入
    @Resource
    private ArticleMapper articleMapper;
//    依赖注入，通过级联查询的mapper

    @Resource
    private ArticleExtendMapper articleExtendMapper;







    @Override
    public void save(Article article) throws CustomerException {
        //执行插入操作
        articleMapper.insert(article);

    }



    @Override
    public List<Article> query(String title) {
        ArticleExample example = new ArticleExample();
        if(title!=null){
            example.createCriteria().andTitleLike("%"+title+"%");
        }
        List<Article> list=articleMapper.selectByExample(example);
        return list;
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        //删除操作
        articleMapper.deleteByPrimaryKey(id);

    }


    @Override
    public List<ArticleExtend> queryCatogory(String title, String status, Long categoryId) {
      return   articleExtendMapper.selectWithCategory(title,status,categoryId);
    }


    //使用一对多进行级联查询
    @Override
    public ArticleExtend read(Long id) {
        return articleExtendMapper.selectById(id);
    }
}
